#pragma once

#include <QString>
#include <QIcon>
#include <QByteArray>
#include <QUuid>
#include <QDateTime>
#include <QFile>
#include <QFileInfo>


namespace model
{
// 宏日志
#define TAG QString("[%1:%2]").arg(model::getFileName(__FILE__), QString::number(__LINE__))
#define LOG() qDebug() << TAG

static inline QString getFileName(const QString& path)
{
    QFileInfo fileinfo(path);
    return fileinfo.fileName();
}

// 工具函数
static inline int64_t getTime()
{
    // 获取当前秒级时间戳
    return QDateTime::currentSecsSinceEpoch();
}

static inline QString formatTime(int64_t timestamp)
{
    // 1. 获取当前时间戳并转换为 QDateTime 对象
    // QDateTime temp = QDateTime::fromMSecsSinceEpoch(timestamp); // 毫秒级
    QDateTime temp = QDateTime::fromSecsSinceEpoch(timestamp);
    // 2. 输出格式化时间串
    return temp.toString("MM-dd HH:mm:ss"); // 月-日 时:分:秒
}

// 用户信息
class UserInfo
{
public:
    QString userId = "";
    QString nickname = ""; // 昵称
    QString phonenum = ""; // 电话
    QString signature = ""; // 用户签名
    QIcon avatar; // 头像
};


// 消息类型
enum MessageType
{
    TEXT_MSG,  // 文本消息
    IMAGE_MSG, // 图片消息
    FILE_MSG,  // 文件消息
    SPEECH_MSG // 语音消息
};



// 消息信息
class Message
{
public:
    QString messageId = "";  // 消息编号
    QString sessionId = "";  // 消息所属会话编号
    QString time = "";       // 消息发布时间 格式化时间
    MessageType type = TEXT_MSG;   // 消息类型
    UserInfo sender;      // 发布者信息
    QByteArray content; // 消息正文
    QString fileId = "";     // 文件标识，消息类型为 FILE_MSG 时启用
    QString fileName = "";   // 文件名称

    // 工厂模式
    static Message makeMessage(MessageType type, const QString& chatSessionId, const UserInfo& sender,
                               const QByteArray& content, const QString& extraInfo)
    {
        if (type == TEXT_MSG)
        {
            return makeTextMessage(chatSessionId, sender, content);
        }
        else if (type == IMAGE_MSG)
        {
            return makeImagetMessage(chatSessionId, sender, content);
        }
        else if (type == FILE_MSG)
        {
            return makeFiletMessage(chatSessionId, sender, content, extraInfo);
        }
        else if (type == SPEECH_MSG)
        {
            return makeSpeechMessage(chatSessionId, sender, content);
        }
        else // 未知的消息类型
        {
            return Message();
        }
    }

private:
    static QString makeId()
    {
        // 提取 Uuid 的最后一段
        return "MSG-" + QUuid::createUuid().toString().sliced(25, 12);
    }

    static Message makeTextMessage(const QString& chatSessionId, const UserInfo& sender, const QByteArray& content)
    {
        Message message;

        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.time = model::formatTime(getTime());
        message.sender = sender;
        message.content = content;
        message.type = TEXT_MSG;

        return message;
    }

    static Message makeImagetMessage(const QString& chatSessionId, const UserInfo& sender, const QByteArray& content)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.time = model::formatTime(getTime());
        message.sender = sender;
        message.content = content;
        message.type = IMAGE_MSG;

        return message;
    }

    static Message makeFiletMessage(const QString& chatSessionId, const UserInfo& sender, const QByteArray& content,
                             const QString& filename)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.time = model::formatTime(getTime());
        message.sender = sender;
        message.content = content;
        message.type = FILE_MSG;
        message.fileName = filename;

        return message;
    }

    static Message makeSpeechMessage(const QString& chatSessionId, const UserInfo& sender, const QByteArray& content)
    {
        Message message;
        message.messageId = makeId();
        message.sessionId = chatSessionId;
        message.time = model::formatTime(getTime());
        message.sender = sender;
        message.content = content;
        message.type = SPEECH_MSG;

        return message;
    }
};


// 会话信息
class ChatSessionInfo
{
public:
    QString sessionId = "";    // 会话编号
    QString sessionName = "";  // 会话名称，用户/群聊 名称
    Message message;        // 最新一条消息
    QIcon avatar ;          // 会话头像，用户/群聊 头像

};

} // end of model
